<template>
	<el-dialog v-model="isShowDialog" :title="'门店用户'" :width="1200" draggable="">
		<div class="busStoreUser-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="门店Id">
          <el-select clearable="" filterable="" v-model="queryParams.storeId" placeholder="请选择门店Id">
            <el-option v-for="(item,index) in busStroeDropdownList" :key="index" :value="item.value" :label="item.label" />
            
          </el-select>
          
        </el-form-item>
        <el-form-item label="用户账号Id">
          <el-select clearable="" filterable="" v-model="queryParams.loginId" placeholder="请选择用户账号Id">
            <el-option v-for="(item,index) in sysUserDropdownList" :key="index" :value="item.value" :label="item.label" />
            
          </el-select>
          
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'busStoreUser:page'"> 查询 </el-button>
            <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
            
          </el-button-group>
          
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Plus" @click="openAddBusStoreUser" v-auth="'busStoreUser:add'"> 新增 </el-button>
          
        </el-form-item>
        
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
        <el-table-column type="index" label="序号"   width="55" align="center"/>
         <el-table-column prop="id" label="主键Id"   show-overflow-tooltip="" />
         <el-table-column prop="tenantId" label="租户Id"   show-overflow-tooltip="" />
        <el-table-column prop="storeId" label="门店Id" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.busStroeSName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="loginId" label="用户账号Id" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.sysUserRealName}}</span>
            
          </template>
          
        </el-table-column>
         <el-table-column prop="createTime" label="创建时间"   show-overflow-tooltip="" />
         <el-table-column prop="updateTime" label="更新时间"   show-overflow-tooltip="" />
         <el-table-column prop="createUserId" label="创建者Id"   show-overflow-tooltip="" />
         <el-table-column prop="updateUserId" label="修改者Id"   show-overflow-tooltip="" />
         <el-table-column prop="createUserName" label="创建者"   show-overflow-tooltip="" />
        <el-table-column prop="updateUserName" label="修改者" show-overflow-tooltip=""/>
        <el-table-column prop="isDelete" label="软删除" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.isDelete"> 是 </el-tag>
            <el-tag type="danger" v-else=""> 否 </el-tag>
            
          </template>
          
        </el-table-column>
        <el-table-column label="操作" width="200" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('busStoreUser:edit') || auth('busStoreUser:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditBusStoreUser(scope.row)" v-auth="'busStoreUser:edit'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delBusStoreUser(scope.row)" v-auth="'busStoreUser:delete'"> 停用 </el-button>
            <el-button icon="ele-Check" size="small" text="" type="primary" @click="isEnable(scope.row)" v-auth="'busStoreUser:page'"> 启用 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <editDialog
			    ref="editDialogRef"
			    :title="editBusStoreUserTitle"
			    @reloadTable="handleQuery"
      />
    </el-card>
  </div>
	</el-dialog>
</template>

<script lang="ts" setup="" name="busStoreUser">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';
  //import { formatDate } from '/@/utils/formatTime';

  import editDialog from '/@/views/main/busStoreUser/component/editDialog.vue'
  import { pageBusStoreUser, deleteBusStoreUser ,setEnable} from '/@/api/main/busStoreUser';
  import { getBusStroeDropdown } from '/@/api/main/busStoreUser';
  import { getSysUserDropdown } from '/@/api/main/busStoreUser';
import { Row } from "element-plus/es/components/table-v2/src/components";

  const isShowDialog = ref(false);

const openDialog = async (row: any) => {
	isShowDialog.value = true;
	console.log(row.orderCode);
	loading.value = true;
	var res = await pageBusStoreUser(Object.assign(tableParams.value, { storeId: row.id }));
	tableData.value = res.data.result?.items ?? [];
	console.log(res.data.result.items);
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
};

    const editDialogRef = ref();
    const loading = ref(false);
    const tableData = ref<any>
      ([]);
      const queryParams = ref<any>
        ({});
        const tableParams = ref({
        page: 1,
        pageSize: 10,
        total: 0,
        });
        const editBusStoreUserTitle = ref("");


        // 查询操作
        const handleQuery = async () => {
        loading.value = true;
        var res = await pageBusStoreUser(Object.assign(queryParams.value, tableParams.value));
        tableData.value = res.data.result?.items ?? [];
        tableParams.value.total = res.data.result?.total;
        loading.value = false;
        };

        // 打开新增页面
        const openAddBusStoreUser = () => {
        editBusStoreUserTitle.value = '添加门店用户';
        editDialogRef.value.openDialog({});
        };

        // 打开编辑页面
        const openEditBusStoreUser = (row: any) => {
        editBusStoreUserTitle.value = '编辑门店用户';
        editDialogRef.value.openDialog(row);
        };

        // 删除
        const delBusStoreUser = (row: any) => {
        ElMessageBox.confirm(`确定要删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        })
        .then(async () => {
        await deleteBusStoreUser(row);
        handleQuery();
        ElMessage.success("删除成功");
        })
        .catch(() => {});
        };
         // 启用
         const isEnable = async(row: any) => {
         
         await setEnable(row);
         handleQuery();
         ElMessage.success("启用成功");
        
         };

        // 改变页面容量
        const handleSizeChange = (val: number) => {
        tableParams.value.pageSize = val;
        handleQuery();
        };

        // 改变页码序号
        const handleCurrentChange = (val: number) => {
        tableParams.value.page = val;
        handleQuery();
        };

        const busStroeDropdownList = ref<any>([]); 
const getBusStroeDropdownList = async () => {
    let list = await getBusStroeDropdown();
    busStroeDropdownList.value = list.data.result ?? [];
};
getBusStroeDropdownList();

        const sysUserDropdownList = ref<any>([]); 
const getSysUserDropdownList = async () => {
    let list = await getSysUserDropdown();
    sysUserDropdownList.value = list.data.result ?? [];
};
getSysUserDropdownList();


defineExpose({ openDialog });
// handleQuery();
</script>


